#include <iostream>
#include "MysqlConnPool.h"
#include <functional>
using namespace std;
using TASK = function<void()>;
int query()
{
    MysqlConn conn;
    bool ret = conn.connect("zrj", "zrj123", "test", "127.0.0.1");
    // cout << ret << endl;
    // ret = conn.update("insert into students (name, student_id, birthday, score)values ('fff','5555','1998-10-09',406)");
    conn.query("select * from students");
    while (conn.next())
    {
        cout << conn.value(0) << "  "
             << conn.value(1) << "  "
             << conn.value(2) << "  "
             << conn.value(3) << "  "
             << conn.value(4) << endl;
    }
    return 0;
}

void unusePool(int begin, int end)
{
    for (int i = begin; i < end; ++i)
    {
        MysqlConn conn;
        conn.connect("zrj", "zrj123", "test", "127.0.0.1");
        char buf[1024] = {0};
        sprintf(buf, "insert into students (name, student_id, birthday, score)values ('fff','%d','1998-10-09',406)", i);
        conn.update(buf);
    }
}

void usePool(int begin, int end, MysqlConnPool *pool)
{
    for (int i = begin; i < end; ++i)
    {
        shared_ptr<MysqlConn> conn = pool->getConnection();
        char buf[1024] = {0};
        sprintf(buf, "insert into students (name, student_id, birthday, score)values ('fff','%d','1998-10-09',406)", i);
        conn->update(buf);
    }
}
// 单线程
int test1()
{
#if 1
    steady_clock::time_point begin = steady_clock::now();
    unusePool(0, 500);
    steady_clock::time_point end = steady_clock::now();
    auto time = end - begin;
    cout << "不使用连接池，单线程，用时" << time.count() / 10000000 << "毫秒" << endl;
#else
    MysqlConnPool *pool = MysqlConnPool::getMysqlConnPool();
    steady_clock::time_point begin = steady_clock::now();
    usePool(0, 500, pool);
    steady_clock::time_point end = steady_clock::now();
    auto time = end - begin;
    cout << "使用连接池，单线程，用时" << time.count() / 10000000 << "毫秒" << endl;
    pool->exit();
#endif
    return 0;
}

/*------------------------------------------------------------------------------------------------------*/

// 多线程
int test2()
{
#if 0
    MysqlConn conn;
    conn.connect("zrj", "zrj123", "test", "127.0.0.1");
    steady_clock::time_point begin = steady_clock::now();
    thread t1(unusePool, 0, 100);
    thread t2(unusePool, 100, 200);
    thread t3(unusePool, 200, 300);
    thread t4(unusePool, 300, 400);
    thread t5(unusePool, 400, 500);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto time = end - begin;
    cout << "不使用连接池，多线程，用时" << time.count() / 10000000 << "毫秒" << endl;
#else
    MysqlConnPool *pool = MysqlConnPool::getMysqlConnPool();
    steady_clock::time_point begin = steady_clock::now();
    thread t1(usePool, 0, 100, pool);
    thread t2(usePool, 100, 200, pool);
    thread t3(usePool, 200, 300, pool);
    thread t4(usePool, 300, 400, pool);
    thread t5(usePool, 400, 500, pool);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto time = end - begin;
    cout << "使用连接池，多线程，用时" << time.count() / 10000000 << "毫秒" << endl;
    pool->exit();
#endif
    return 0;
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}
